var statusCode = {ok:200, error:300, timeout:301, forbidden:403};
var token = true;

$.ajax2 = function(options, aimDiv) {
    var img = $("<img id=\"progressImgage\" src=\"" + path + "/styles/public/images/loading.gif\" />");
    var mask = $("<div id=\"maskOfProgressImage\"></div>").addClass("mask").addClass("hide");
    var PositionStyle = "fixed";
    // 是否将Loading固定在aimDiv中操作,否则默认为全屏Loading遮罩
    if (aimDiv != null && aimDiv != "" && aimDiv != undefined) {
        $(aimDiv).css("position", "relative").append(img).append(mask);
        PositionStyle = "absolute";
    } else {
        $("body").append(img).append(mask);
    }
    img.css({
        "z-index": "2000",
        "display": "none"
    });
    mask.css({
        "position": PositionStyle,
        "top": "0",
        "right": "0",
        "bottom": "0",
        "left": "0",
        "z-index": "1000",
        "background-color": "#000000",
        "display": "none"
    });
    var complete = options.complete;
    options.complete = function(httpRequest, status) {
    	token = true;
        img.hide();
        mask.hide();
        if (complete) {
            complete(httpRequest, status);
        }
    };
    options.async = true;
    img.show().css({
        "position": PositionStyle,
        "top": "40%",
        "left": "50%",
        "margin-top": function() { return -1 * img.height() / 2; },
        "margin-left": function() { return -1 * img.width() / 2; }
    });
    mask.show().css("opacity", "0.1");
    $.ajax(options);
};

$.get2 = function(url, data, success, dataType, aimDiv) {
	$.ajax2({
		type: 'GET',
        url: url,
        data: data,
		dataType: dataType,
		cache: false,
        success: success
    }, aimDiv);
};

$.post2 = function(url, data, success, dataType, aimDiv) {
	if (!token) {
		return;
	}
	$.ajax2({
		type: 'POST',
        url: url,
        data: data,
		dataType: dataType,
		cache: false,
		beforeSend: function(){
			token = false;
		},
        success: success
    }, aimDiv);
};

/**
 * 普通ajax表单提交
 * @param {Object} form
 * @param {Object} callback
 * @param {Object} aimDiv
 */
function onSubmit(form, callback, aimDiv) {
	if (!token) {
		return false;
	}

	var $form = $(form);

	var _submitFn = function(){
		$.ajax2({
			type: form.method || 'POST',
			url: $form.attr("action"),
			data: $form.serializeArray(),
			dataType: "json",
			async: true,
			cache: false,
			beforeSend: function(){
				token = false;
			},
			success: callback,
			error: function (data, status, e) {
				error(data.responseText);
                return false;
            }
		}, aimDiv);
	};

	_submitFn();
	return false;
}

/**
 * 普通ajax表单提交（字段验证）for validationEngine
 * @param {Object} form
 * @param {Object} callback
 * @param {Object} aimDiv
 */
function validateCallback(form, callback, aimDiv) {
	if (!token) {
		return false;
	}

	var $form = $(form);

	if (!$form.validationEngine('validate')) {
		return false;
	}

	var _submitFn = function(){
		$.ajax2({
			type: form.method || 'POST',
			url: $form.attr("action"),
			data: $form.serializeArray(),
			dataType: "json",
			async: true,
			cache: false,
			beforeSend: function(){
				token = false;
			},
			success: callback,
			error: function (data, status, e) {
				error(data.responseText);
                return false;
            }
		}, aimDiv);
	};

	_submitFn();
	return false;
}

function callback(data) {
	if (data.statusCode == statusCode.ok) {
		success(data.message);
		if (typeof(data.forwardUrl) != "undefined" && data.forwardUrl != "") {
			location.href = data.forwardUrl;
		}
	}
	if (data.statusCode == statusCode.error) {
		error(data.message);
	}
	if (data.statusCode == statusCode.timeout) {
		location.href = path + "/login";
	}
}

function callbackRef(data) {
	if (data.statusCode == statusCode.ok) {
		success(data.message);
    	close2refresh();
	}
	if (data.statusCode == statusCode.error) {
		error(data.message);
	}
	if (data.statusCode == statusCode.timeout) {
		location.href = path + "/login";
	}
}

function success(msg) {
	alert(msg);
}

function error(msg) {
	alert(msg);
}

/**
 * 让form表单在提交前先弹出确认提示
 * @param submitBtn submit按钮type改成button，click事件绑定此方法
 */
function submitForm(submitBtn) {
	confirm("确认要执行此操作吗？") && $(submitBtn).parents("form").submit();
}